package com.character.controller;



import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.character.entity.Menu;
import com.character.entity.MyReturn;
import com.character.entity.RoleMenu;
import com.character.service.MenuService;
import com.character.service.RoleMenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * (Menu)表控制层
 *
 * @author makejava
 * @since 2021-12-03 09:16:47
 */
@RestController
@RequestMapping("menu")
public class MenuController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private MenuService menuService;
    @Resource
    private RoleMenuService roleMenuService;

    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     */
   @RequestMapping("/selectAll")
    public MyReturn selectAll() throws IllegalAccessException {
        List<Tree<String>> treeMenu = menuService.getTreeMenu();
        return super.success(treeMenu);
    }

    @RequestMapping("/selectLayuiTree")
    public MyReturn selectLayuiTree(){
        List<Tree<String>> treeMenu = menuService.getLayuiTree();
        return super.success(treeMenu);
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public MyReturn selectOne(@PathVariable Serializable id) {
        return success(this.menuService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param menu 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    public MyReturn insert(@RequestBody Menu menu) {
        menu.setCreateTime(new Date());
        boolean save = menuService.save(menu);
        if(save){
            return  super.success(menu);
        }else {
            return super.failed(menu);
        }
    }

    /**
     * 修改数据
     *
     * @param menu 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    public MyReturn update(@RequestBody Menu menu) {
        menu.setUpdateTime(new Date());
        boolean updateById = menuService.updateById(menu);
        if(updateById){
            return  super.success(menu);
        }else {
            return super.failed(menu);
        }
    }

    /**
     * 删除数据
     *
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    public MyReturn delete(String menuId) {
        QueryWrapper<RoleMenu> roleMenuQueryWrapper = new QueryWrapper<>();
        roleMenuQueryWrapper.eq("menu_id",menuId);
        boolean remove = roleMenuService.remove(roleMenuQueryWrapper);
        boolean removeById = menuService.removeById(menuId);
        if(removeById&&remove){
            return  super.success(null);
        }else {
            return super.failed(null);
        }
    }
}

